﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<SG.Domain.Categoria>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Listado de categorías
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="TitlePage" runat="server">
	Listado de categorías
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Head" runat="server">
    
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.css") %>" rel="stylesheet" type="text/css" />   
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid_skins.css") %>" rel="stylesheet" type="text/css" />
    <link href="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/skins/dhtmlxgrid_dhx_blue.css") %>" rel="stylesheet" type="text/css" />


    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxcommon.js") %>" type="text/javascript"></script>
     <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgrid.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/dhtmlxgridcell.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/ext/dhtmlxgrid_filter.js") %>" type="text/javascript"></script>
    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Delete", "Categoria", FormMethod.Post, new  { id="formDelete"}))
    {%>
   <div id="entityContainer"></div>
 <%} %>   

 <%
      
       string array = "";
       foreach (var item in Model)
       {
           array += ",['0','"+
                        item.Id + "','" +   
                        item.Nombre + "','" +
                        item.LimiteInferior + "','" +
                        item.LimiteSuperior + "','" +
                        item.AgotamientoAnualPermitido + "','" +
                    
                    "']";
                       
       }
       if (string.IsNullOrEmpty(array))
           array = "Array()";
       else 
           array =  "["+array.Substring(1)+"]";
       
        %>
              
            <input type="button" value="Eliminar" onclick="removeRows();"/>
            <div id="gridList" style="height:100%"></div>
    <p>
        <%: Html.ActionLink("Crear", "CreateEdit") %>
    </p>
    
    <script type="text/javascript">
      var jarray = <%=(array??"new array()") %>;

        var mygrid;
      
          var winx; 
          window.onload=doOnLoad;
        function doOnLoad(){
           
            mygrid = new dhtmlXGridObject('gridList');
            mygrid.setImagePath("<%=Url.Content("~/Scripts/dhtmlx/dhtmlxGrid/codebase/imgs/") %>");
            mygrid.setHeader(",,Nombre,Límite inferior,Límite superior,Agotamiento anual permitido",null,["text-align:center","text-align:center;","text-align:center;","text-align:center;","text-align:center;","text-align:center;"]);
            mygrid.attachHeader("<input id='checkAllGrid' type='checkbox' onclick='checkAll(this);'/>, ,#text_filter,#numeric_filter,#numeric_filter,#numeric_filter");
            mygrid.setInitWidths("40,0,*,120,120,120");
            mygrid.setColAlign("center,left,left,left,left,left");
            mygrid.setColTypes("ch,ed,ed,ed,ed,ed");
            mygrid.setColumnIds("CHECK,ID,NOMBRE,LIMITEINFERIOR,LIMITESUPERIOR,AGOTAMIENTOANUALPERMITIDO");
            mygrid.enableEditEvents(false,false,false);
            mygrid.setSkin("light");
            mygrid.setColSorting("int,str,str,str,str,str");
            
            mygrid.enableAutoHeight(true);
            mygrid.attachEvent("onRowDblClicked", function(rId,cInd){
                window.location="<%=Url.Action("CreateEdit")%>/"+mygrid.cells(rId,1).getValue()+"";
            });  

            
            mygrid.init();
            mygrid.parse(jarray, "jsarray");


          
           
        }
         function removeRows() {
            var xx=mygrid.getAllRowIds(",");
            xx= xx.split(",");
            var haySeleccinados=false;
            for(i=0;i<xx.length;i++){
                if(mygrid.cells(xx[i],0).getValue(0)==1){
                   $("#entityContainer").append("<input name='IdEntity' type='hidden' value='"+mygrid.cells(xx[i],1).getValue()+"'/>");
                   haySeleccinados=true;
                   }
            }
            if(!haySeleccinados)
                alert("Debe seleccionar al menos una categoría");
            else
               document.forms["formDelete"].submit();

        }
          function checkAll(obj){
            window.event.cancelBubble = true;
            var xx=mygrid.getAllRowIds(",");
            xx= xx.split(",");
            for(i=0;i<xx.length;i++){
                 mygrid.cells(xx[i],0).setValue(obj.checked?1:0);
            }
        }
    </script>
</asp:Content>

